import datetime
import os.path
import sys

from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from webdriver_manager.core.os_manager import ChromeType
from webdriver_manager.microsoft import EdgeChromiumDriverManager

# 代理
# import os
# os.environ['WDM_PROXY'] = 'http://proxy.example.com:8080'


class Driver:
    driver = ""

    def __init__(self):
        # 使用国内镜像源加速下载
        # driver_binary = EdgeChromiumDriverManager(
        #     url="https://npmmirror.com/mirrors/edgewebdriver",
        #     latest_release_url="https://npmmirror.com/mirrors/edgewebdriver/LATEST_RELEASE"
        # ).install()
        #
        # service = Service(driver_binary)
        # self.driver = webdriver.Edge(service=service)

        self.driver_path = EdgeChromiumDriverManager().install()
        self.edge_options = Options()
        self.edge_options.add_argument("--remote-allow-origins=*")
        self.driver = webdriver.Edge(
            service=Service(self.driver_path),
            options=self.edge_options)

        # 设置隐式等待时间

    # 获取屏幕截图
    def getscreeshot(self):
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        # 是否有该文件夹，不存在则创建文件夹
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)

        # sys.xxx 获取调用的方法名
        filename = sys._getframe().f_back.f_code.co_name + "---" + datetime.datetime.now().strftime(
            "%Y-%m-%d-%H%M%S") + ".png"
        # 屏幕截图
        self.driver.save_screenshot("../images/" + dirname + "/" + filename)


FSDriver = Driver()
